package com.module.base.widget.skin.s

import android.content.Context
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import com.hjq.shape.builder.ButtonDrawableBuilder
import com.hjq.shape.builder.ShapeDrawableBuilder
import com.hjq.shape.builder.TextColorBuilder
import com.hjq.shape.layout.*
import com.hjq.shape.styleable.ICompoundButtonStyleable
import com.hjq.shape.styleable.IShapeDrawableStyleable
import com.hjq.shape.styleable.ITextColorStyleable
import com.hjq.shape.view.*
import com.module.base.R
import skin.support.content.res.SkinCompatResources
import skin.support.widget.SkinCompatHelper

/**
 * Author: yanxuwen
 * Date: 2023/4/23
 * Desc: ShapeView 主题皮肤统一处理
 *  * 颜色渐变暂时不处理
 */
class SkinShapeImpl(
    var context: Context,
    var builder: ShapeDrawableBuilder,
    var styleable: IShapeDrawableStyleable,
    var textBuilder: TextColorBuilder?,
    var textStyleable: ITextColorStyleable?,
    var compoundBuilder: ButtonDrawableBuilder?,
    var compoundStyleable: ICompoundButtonStyleable?
) {

    /**填充背景*/
    private var solidColor = 0
    private var solidPressedColor = 0
    private var solidCheckedColor = 0
    private var solidDisabledColor = 0
    private var solidFocusedColor = 0
    private var solidSelectedColor = 0

    /**边框*/
    private var strokeColor = 0
    private var strokePressedColor = 0
    private var strokeCheckedColor = 0
    private var strokeDisabledColor = 0
    private var strokeFocusedColor = 0
    private var strokeSelectedColor = 0

    /**字体颜色*/
    private var textColor = 0
    private var textPressedColor = 0
    private var textCheckedColor = 0
    private var textDisabledColor = 0
    private var textFocusedColor = 0
    private var textSelectedColor = 0

    /**阴影颜色*/
    private var shadowColor = 0

    /**图片*/
    private var buttonDrawable = 0
    private var buttonPressedDrawable = 0
    private var buttonCheckedDrawable = 0
    private var buttonDisabledDrawable = 0
    private var buttonFocusedDrawable = 0
    private var buttonSelectedDrawable = 0


    fun initTypedArray(view: View, attrs: AttributeSet?) {
        if (attrs == null) {
            return
        }
        val INVALID_ID = 0
        val typedArray: TypedArray =
            when (view) {
                is ShapeButton -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeButton, 0, 0)
                }
                is ShapeCheckBox -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeCheckBox, 0, 0)
                }
                is ShapeEditText -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeEditText, 0, 0)
                }
                is ShapeImageView -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeImageView, 0, 0)
                }
                is ShapeRadioButton -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeRadioButton, 0, 0)
                }
                is ShapeTextView -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView, 0, 0)
                }
                is ShapeView -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeView, 0, 0)
                }
                is ShapeConstraintLayout -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeConstraintLayout, 0, 0)
                }
                is ShapeFrameLayout -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeFrameLayout, 0, 0)
                }
                is ShapeLinearLayout -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeLinearLayout, 0, 0)
                }
                is ShapeRadioGroup -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeRadioGroup, 0, 0)
                }
                is ShapeRecyclerView -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeRecyclerView, 0, 0)
                }
                is ShapeRelativeLayout -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeRelativeLayout, 0, 0)
                }
                else -> {
                    context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView, 0, 0)
                }
            }

        /**填充背景*/
        solidColor =
            typedArray.getResourceId(styleable.solidColorStyleable, INVALID_ID)
        solidPressedColor =
            typedArray.getResourceId(styleable.solidPressedColorStyleable, INVALID_ID)
        solidCheckedColor =
            typedArray.getResourceId(styleable.solidCheckedColorStyleable, INVALID_ID)
        solidDisabledColor =
            typedArray.getResourceId(styleable.solidDisabledColorStyleable, INVALID_ID)
        solidFocusedColor =
            typedArray.getResourceId(styleable.solidFocusedColorStyleable, INVALID_ID)
        solidSelectedColor =
            typedArray.getResourceId(styleable.solidSelectedColorStyleable, INVALID_ID)
        /**边框*/
        strokeColor =
            typedArray.getResourceId(styleable.strokeColorStyleable, INVALID_ID)
        strokePressedColor =
            typedArray.getResourceId(styleable.strokePressedColorStyleable, INVALID_ID)
        strokeCheckedColor =
            typedArray.getResourceId(styleable.strokeCheckedColorStyleable, INVALID_ID)
        strokeDisabledColor =
            typedArray.getResourceId(styleable.strokeDisabledColorStyleable, INVALID_ID)
        strokeFocusedColor =
            typedArray.getResourceId(styleable.strokeFocusedColorStyleable, INVALID_ID)
        strokeSelectedColor =
            typedArray.getResourceId(styleable.strokeSelectedColorStyleable, INVALID_ID)
        /**字体颜色*/
        textStyleable?.let {
            textColor =
                typedArray.getResourceId(it.textColorStyleable, INVALID_ID)
            textPressedColor =
                typedArray.getResourceId(it.textPressedColorStyleable, INVALID_ID)
            textCheckedColor =
                typedArray.getResourceId(it.textCheckedColorStyleable, INVALID_ID)
            textDisabledColor =
                typedArray.getResourceId(it.textDisabledColorStyleable, INVALID_ID)
            textFocusedColor =
                typedArray.getResourceId(it.textFocusedColorStyleable, INVALID_ID)
            textSelectedColor =
                typedArray.getResourceId(it.textSelectedColorStyleable, INVALID_ID)
        }
        /**阴影颜色*/
        shadowColor =
            typedArray.getResourceId(styleable.shadowColorStyleable, INVALID_ID)
        /**图片*/
        compoundStyleable?.let {
            buttonDrawable = typedArray.getResourceId(it.buttonDrawableStyleable, INVALID_ID)
            buttonPressedDrawable = typedArray.getResourceId(it.buttonPressedDrawableStyleable, INVALID_ID)
            buttonCheckedDrawable = typedArray.getResourceId(it.buttonCheckedDrawableStyleable, INVALID_ID)
            buttonDisabledDrawable = typedArray.getResourceId(it.buttonDisabledDrawableStyleable, INVALID_ID)
            buttonFocusedDrawable = typedArray.getResourceId(it.buttonFocusedDrawableStyleable, INVALID_ID)
            buttonSelectedDrawable = typedArray.getResourceId(it.buttonSelectedDrawableStyleable, INVALID_ID)
        }

        typedArray.recycle()

    }

    fun applyBorderColorResource() {
        val INVALID_ID = 0
        /**填充背景*/
        setSkinColor(solidColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.solidColor = color
            }
        })
        setSkinColor(solidPressedColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.solidPressedColor = color
            }
        })
        setSkinColor(solidCheckedColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.solidCheckedColor = color
            }
        })
        setSkinColor(solidDisabledColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.solidDisabledColor = color
            }
        })
        setSkinColor(solidFocusedColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.solidFocusedColor = color
            }
        })
        setSkinColor(solidSelectedColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.solidSelectedColor = color
            }
        })
        /**边框*/
        setSkinColor(strokeColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.strokeColor = color
            }
        })
        setSkinColor(strokePressedColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.strokePressedColor = color
            }
        })
        setSkinColor(strokeCheckedColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.strokeCheckedColor = color
            }
        })
        setSkinColor(strokeDisabledColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.strokeDisabledColor = color
            }
        })
        setSkinColor(strokeFocusedColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.strokeFocusedColor = color
            }
        })
        setSkinColor(strokeSelectedColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.strokeSelectedColor = color
            }
        })
        /**字体颜色*/
        textBuilder?.let {
            setSkinColor(textColor, object : OnSuccessListener {
                override fun onSuccess(color: Int) {
                    it.textColor = color
                }
            })
            setSkinColor(textPressedColor, object : OnSuccessListener {
                override fun onSuccess(color: Int) {
                    it.textPressedColor = color
                }
            })
            setSkinColor(textCheckedColor, object : OnSuccessListener {
                override fun onSuccess(color: Int) {
                    it.textCheckedColor = color
                }
            })
            setSkinColor(textDisabledColor, object : OnSuccessListener {
                override fun onSuccess(color: Int) {
                    it.textDisabledColor = color
                }
            })
            setSkinColor(textFocusedColor, object : OnSuccessListener {
                override fun onSuccess(color: Int) {
                    it.textFocusedColor = color
                }
            })
            setSkinColor(textSelectedColor, object : OnSuccessListener {
                override fun onSuccess(color: Int) {
                    it.textSelectedColor = color
                }
            })
            it.intoTextColor()
        }
        /**阴影颜色*/
        setSkinColor(shadowColor, object : OnSuccessListener {
            override fun onSuccess(color: Int) {
                builder.shadowColor = color
            }
        })
        /**图片*/
        compoundBuilder?.let {
            setSkinDrawable(buttonDrawable, object : OnSuccessListener2 {
                override fun onSuccess(drawable: Drawable) {
                    it.buttonDrawable = drawable
                }
            })
            setSkinDrawable(buttonPressedDrawable, object : OnSuccessListener2 {
                override fun onSuccess(drawable: Drawable) {
                    it.drawablePressedDrawable = drawable
                }
            })
            setSkinDrawable(buttonCheckedDrawable, object : OnSuccessListener2 {
                override fun onSuccess(drawable: Drawable) {
                    it.drawableCheckedDrawable = drawable
                }
            })
            setSkinDrawable(buttonDisabledDrawable, object : OnSuccessListener2 {
                override fun onSuccess(drawable: Drawable) {
                    it.drawableDisabledDrawable = drawable
                }
            })
            setSkinDrawable(buttonFocusedDrawable, object : OnSuccessListener2 {
                override fun onSuccess(drawable: Drawable) {
                    it.drawableFocusedDrawable = drawable
                }
            })
            setSkinDrawable(buttonSelectedDrawable, object : OnSuccessListener2 {
                override fun onSuccess(drawable: Drawable) {
                    it.drawableSelectedDrawable = drawable
                }
            })
            it.intoButtonDrawable()
        }
        builder.intoBackground()

    }

    private fun setSkinColor(color: Int?, listener: OnSuccessListener) {
        if (color == null) {
            return
        }
        val INVALID_ID = 0
        var color = SkinCompatHelper.checkResourceId(color)
        if (color != INVALID_ID) {
            if (listener != null) {
                listener.onSuccess(SkinCompatResources.getColor(context, color))
            }
        }
    }

    private fun setSkinDrawable(res: Int?, listener: OnSuccessListener2) {
        if (res == null) {
            return
        }
        val INVALID_ID = 0
        var color = SkinCompatHelper.checkResourceId(res)
        if (color != INVALID_ID) {
            if (listener != null) {
                listener.onSuccess(SkinCompatResources.getDrawable(context, res))
            }
        }
    }

    interface OnSuccessListener {
        fun onSuccess(color: Int);
    }

    interface OnSuccessListener2 {
        fun onSuccess(drawable: Drawable);
    }

    fun applySkin() {
        applyBorderColorResource()
    }
}